package com.umerclass.ui.home.adapter;

import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.umerclass.R;
import com.umerclass.bean.home.VideoBean;
import com.umerclass.frame.ui.recycleview.listener.OptListener;
import java.util.List;

public class VideoAdapterTwo extends RecyclerView.Adapter {

    private static final int ITEM_VIEW_TYPE_HEADER = 0;
    private static final int ITEM_VIEW_TYPE_ITEM = 1;
    private List<VideoBean> dataList;
    private OptListener optListener;

    public VideoAdapterTwo(OptListener optListener,List<VideoBean> dataList){
        this.dataList = dataList;
        this.optListener = optListener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == ITEM_VIEW_TYPE_HEADER) {
            return new HeaderHolder(LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.video_item_one, parent, false));
        } else {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.video_item_two, parent, false);
            return new ItemHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == ITEM_VIEW_TYPE_HEADER) {
            HeaderHolder headerHolder = (HeaderHolder) holder;
            VideoBean videoBean =(VideoBean)  dataList.get(position);
            String url = videoBean.getPath();
            headerHolder.simpleDraweeView.setImageURI(Uri.parse(url));
            headerHolder.tv1.setText(videoBean.getTxt());
            headerHolder.tv2.setText(videoBean.getPeople()+"人看过");
            headerHolder.iv_bofang.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    optListener.onOptClick(v,videoBean);
                }
            });
        } else {

            ItemHolder itemHolder = (ItemHolder) holder;
            VideoBean videoBean =(VideoBean)  dataList.get(position);
            String url = videoBean.getPath();
            itemHolder.simpleDraweeView2.setImageURI(Uri.parse(url));
            itemHolder.tv_one.setText(videoBean.getTxt());
            itemHolder.tv_two.setText(videoBean.getTxt()+"人看过");
            itemHolder.tv_three.setText(videoBean.getPeople()+"人看过");
            itemHolder.iv_bofang2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    optListener.onOptClick(v,videoBean);
                }
            });
        }
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getItemCount() {
        int count = dataList != null ? dataList.size() : 0;
        return count;
    }

    @Override
    public int getItemViewType(int position) {
        if(position==0){
            return ITEM_VIEW_TYPE_HEADER;
        }else {
            return ITEM_VIEW_TYPE_ITEM;
        }
    }

    public class HeaderHolder extends RecyclerView.ViewHolder {
        private SimpleDraweeView simpleDraweeView;
        private TextView tv1;
        private TextView tv2;
        private ImageView iv_bofang;

        public HeaderHolder(View itemView) {
            super(itemView);
            simpleDraweeView = itemView.findViewById(R.id.simpleDraweeView);
            tv1 = itemView.findViewById(R.id.tv1);
            tv2 = itemView.findViewById(R.id.tv2);
            iv_bofang = itemView.findViewById(R.id.iv_bofang);
        }
    }

    public class ItemHolder extends RecyclerView.ViewHolder {
        private SimpleDraweeView simpleDraweeView2;
        private TextView tv_one;
        private TextView tv_two;
        private TextView tv_three;
        private ImageView iv_bofang2;
        public ItemHolder(View itemView) {
            super(itemView);
            simpleDraweeView2 = itemView.findViewById(R.id.simpleDraweeView2);
            tv_one = itemView.findViewById(R.id.tv_one);
            tv_two = itemView.findViewById(R.id.tv_two);
            tv_three = itemView.findViewById(R.id.tv_three);
            iv_bofang2 = itemView.findViewById(R.id.iv_bofang2);
        }
    }
}
